home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 029a / masview.zip / TTT5.C < prev   
Text File  |  1991-10-23  |  1KB  |  58 lines

  1. /* ttt5.c - computers move */
  2.  
  3. #include "stdio.h"
  4. #include "ttt.h"
  5.  
  6. extern char board[ 3 ][ 3 ];        /* main playing board */
  7.  
  8. computer_move()
  9. {
  10.     char bd[ 3 ][ 3 ];        /* computers board for testing moves */
  11.     static struct {            /* good moves in strategic order */
  12.         int row;
  13.         int col;
  14.     } gm[] = { 1,1, 0,0, 0,2, 2,0, 2,2, 0,1, 1,0, 1,2, 2,1 };
  15.     int x, y;
  16.  
  17.     for ( x = 0; x < 3; ++x )    /* copy main board */
  18.         for ( y = 0; y < 3; ++y )
  19.             bd[ x ][ y ] = board[ x ][ y ];
  20.     for ( x = 0; x < 3; ++x )    /* win if I can */
  21.         for ( y = 0; y < 3; ++y )
  22.             if ( bd[ x ][ y ] == EMPTY )
  23.             {
  24.                 bd[ x ][ y ] = COMPUTER;
  25.                 if ( winner( bd ) )
  26.                 {
  27.                     cmove( x, y );
  28.                     return;
  29.                 }
  30.                 bd[ x ][ y ] = EMPTY;
  31.             }
  32.     for ( x = 0; x < 3; ++x )    /* else stop human from winning */
  33.         for ( y = 0; y < 3; ++y )
  34.             if ( bd[ x ][ y ] == EMPTY )
  35.             {
  36.                 bd[ x ][ y ] = HUMAN;
  37.                 if ( winner( bd ) )
  38.                 {
  39.                     cmove( x, y );
  40.                     return;
  41.                 }
  42.                 bd[ x ][ y ] = EMPTY;
  43.             }
  44.     for ( x = 0; x <= 8; ++x )    /* else pick best move available */
  45.         if ( board[ gm[ x ].row ][ gm[ x ].col ] == EMPTY )
  46.         {
  47.             cmove( gm[ x ].row, gm[ x ].col );
  48.             return;
  49.         }
  50. }
  51.  
  52. static cmove( x, y )
  53. int x, y;
  54. {
  55.     board[ x ][ y ] = COMPUTER;
  56.     printf( "\nMy move is %c%c\n", x + '1', y + 'A' );
  57. }
  58.